/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Salem_NotePad;

import java.awt.*;
import java.awt.event.*;
import javax.jws.soap.SOAPBinding.Style;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;


/**
 *
 * @author Ahmed Salem�
 */
public class SelectFont extends JFrame implements ActionListener{

   public   JList effectlist;
   public static   JList Sizelist;
 private   JTextField f1;
 private   JLabel l1,l2;
 static    JTextPane ta;
 private   JPanel p1,p2,p3;
 public   JButton b1,b2;
 public   String m_fontName = "";
  JRadioButton b;  
     
     private Container c;
     GraphicsEnvironment ge = GraphicsEnvironment.
     getLocalGraphicsEnvironment();
     String[] fontNames = ge.getAvailableFontFamilyNames();
     String fontstyl[]={"Italic","Bold","Plain"};
 int font[]= new int[50];
     private Color colors[] =  { 
         Color.black, Color.blue, Color.cyan, Color.darkGray,
         Color.gray, Color.green, Color.lightGray,
         Color.magenta, Color.orange, Color.pink, Color.red,
         Color.white, Color.yellow};
    
    
   public String []Sizes =new String[] {"8", "9", "10","11","12","13" ,"14","15","16","17","18",
                                          "19","20","21","22","23","24","25","26","27","28","29","30",
                                          "31","32","33","34","35","36","37","38","39","40","41","42",
                                          "43","44","45","46","47","48","49","50","51","52","53","54",
                                          "55","56","57","58","59","60","61","62","63","64","65","66",
                                          "67","68","69","70","71","72","73","74"};
    private JList lstyl;
                            
     Main f;
     public SelectFont(Main f)
         {
         super( "Select Font ... " );
                 this.f=f;
        this.toFront();
        setVisible(true);
     p1 = new JPanel();
     p2 = new JPanel();
     p3 = new JPanel();
      b =new JRadioButton("OK") ;  
     b1 = new JButton("Ok");
     
     b2 = new JButton("Cancel");               
         // create a list with the items in the colorNames array
         effectlist = new JList(fontNames);        
         ta = new JTextPane();
         
         effectlist.setVisibleRowCount( 5 );
         Sizelist   = new JList(Sizes);
         Sizelist.setVisibleRowCount( 5 );
         
          lstyl=new JList(fontstyl);
          lstyl.setVisibleRowCount(2);    
          lstyl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);           
          lstyl.setSelectedIndex(0);
          
      
         f1 = new JTextField(25);
         l2 = new JLabel("Efficts...");
         l1 = new JLabel("Size...");
           
        ta.setText("                   Note Pad - -                           \n Ahmed Salem");
         ta.setEditable(false);
       
        p1.add(effectlist);        
        p1.add(Sizelist);     
        p2.add(ta);
        p3.add(b1);
        b.setSelected(false);
        p3.add(b2);
 
         // do not allow multiple selections
         effectlist.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
         effectlist.setSelectedIndex(0);
         Sizelist.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
         Sizelist.setSelectedIndex(10);
         // add a JScrollPane containing the JList
         // to the content pane
         p1.add( new JScrollPane( effectlist ) );
         p1.add(new JScrollPane(lstyl));
         p1.add( new JScrollPane( Sizelist ) );
         
    add(p1,BorderLayout.BEFORE_FIRST_LINE);   
   	p1.setLayout(new GridLayout(0,4));
   	p1.setBorder(new TitledBorder (new LineBorder (Color.blue,3)," Effict of Font"));
 
   	p1.setLayout(new GridLayout(1,3));
        
         add(p2,BorderLayout.BEFORE_LINE_BEGINS); 
        p2.setBackground(Color.white);
         p2.setBorder(new TitledBorder (new LineBorder (Color.RED,3),"Privew"));	
   	 p2.setLayout(new FlowLayout(FlowLayout.CENTER));
   	    
        add(p3,BorderLayout.AFTER_LAST_LINE);
        p3.setBorder(new TitledBorder(" Action ..."  ));
        p3.setBackground(Color.BLUE);
        p3.setBorder(new SoftBevelBorder (SoftBevelBorder.RAISED));	
 
             	      
 //********************************************************************************************************              	      
         // set up event handler
   effectlist.addListSelectionListener( new ListSelectionListener()
             {
             public void valueChanged( ListSelectionEvent e )
        
             {
             
     for(int i=0;i<50 ; i++){
     font[i]= Integer.parseInt(Sizes[i]);  
    
     	
     Font ff=new Font(fontNames[effectlist.getSelectedIndex()],10,font[Sizelist.getSelectedIndex() ]); 
     ta.setFont(ff);	 	
        
     }
             }
         } );
 //********************************************************************************************************       
  Sizelist.addListSelectionListener( new ListSelectionListener()
             {
             public void valueChanged( ListSelectionEvent e )
                 {
     int font[]= new int[67];
     for(int i=0;i<67 ; i++){
     font[i]= Integer.parseInt(Sizes[i]);      	
     Font ff=new Font(fontNames[effectlist.getSelectedIndex()],10,font[Sizelist.getSelectedIndex() ]); 
     ta.setFont(ff);	 	
    	
     }
             }
         } );
lstyl.addListSelectionListener( new ListSelectionListener()
             {
             public void valueChanged( ListSelectionEvent e )
                 {
     int font[]= new int[67];
     for(int i=0;i<67 ; i++){
     font[i]= Integer.parseInt(Sizes[i]);      	
  ;
     Font ff=new Font(fontNames[effectlist.getSelectedIndex()],   lstyl.getSelectedIndex(),font[Sizelist.getSelectedIndex() ]); 
     ta.setFont(ff);	 	
    	
     }
             }
         } );         

 //********************************************************************************************************
         
         b1.addActionListener(this);       
         b2.addActionListener(this);     
         setSize( 450, 350 );
         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ;
          
     }

  public int def_style(int index){
        int style;
    if(index==0)
        style=Font.ITALIC;
    else if(index==1)
    style=Font.BOLD;
    else
        style=Font.PLAIN;
    return style;
    }//def_style
public void setfont(){
StyledDocument doc=f.matcher.getStyledDocument();

        javax.swing.text.Style st=StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
        javax.swing.text.Style regular = doc.addStyle("regular", st);
StyleConstants.setFontFamily(regular,effectlist.getSelectedValue().toString());
StyleConstants.setFontSize(regular,Integer.parseInt(Sizelist.getSelectedValue().toString()));


f.matcher.setCharacterAttributes(  doc.getStyle("regular"),true);

               
        


}//setfont

 
 //************************             Actions                  *************************** 
 
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b1){
            setfont();
            this.dispose();
        }
        if(e.getSource()==b2){
             setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        }
    }
      
}